package com.rent.utils;

import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static String DRIVER ;
    private static String URL;
    private static String USER;
    private static String PWD;

    Connection connection = null;

    static{
        Properties properties = new Properties();
        try {
            properties.load(BaseDao.class.getClassLoader().getResourceAsStream("db.properties"));
            DRIVER = properties.getProperty("driver");
            URL = properties.getProperty("url");
            USER = properties.getProperty("user");
            PWD = properties.getProperty("pwd");
            Class.forName(DRIVER);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     * @return
     */
    public Connection getConnection(){
        if(connection==null){
            try {
                connection = DriverManager.getConnection(URL,USER,PWD);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return connection;
    }

    /**
     * 关闭资源
     * @param conn
     * @param statement
     * @param resultSet
     */
    public void closeAll(Connection conn, Statement statement, ResultSet resultSet){
        try {
            if(resultSet!=null&&resultSet.isClosed()){
                resultSet.close();
            }
            if(statement!=null&&statement.isClosed()){
                statement.close();
            }
            if(conn!=null&&conn.isClosed()){
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    

    /**
     * 执行增删改
     * @param sql 语句
     * @param objects 参数
     * @return 返回执行成功的行数
     */
    public int executeUpdate(String sql,Object...objects){
        PreparedStatement ps = null;
        int num = 0;
        try {
            ps = getConnection().prepareStatement(sql);
            if(objects!=null){
                for (int i = 0; i < objects.length; i++) {
                    ps.setObject(i+1,objects[i]);
                }
            }
            num = ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally{
            closeAll(connection,ps,null);
        }
        return num;
    }
    /**
     * 执行查询
     * @param sql 语句
     * @param objects 参数
     * @return 返回resultSet结果集
     */
    public ResultSet executeQuery(String sql,Object...objects){
        PreparedStatement ps = null;
        ResultSet r = null;
        try {
            ps = getConnection().prepareStatement(sql);
            if(objects!=null){
                for (int i = 0; i < objects.length; i++) {
                    ps.setObject(i+1,objects[i]);
                }
            }
            r = ps.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return r;
    }
    
    public void closeAll(ResultSet rs) {
    	try {
			closeAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }

}
